package com.hl.card.modular.packBack.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hl.card.common.persistence.model.Enrolment;
import com.hl.card.common.util.ResponseObject;
import com.hl.card.common.util.ResponseUtil;
import com.hl.card.modular.packBack.service.IEnrolmentService;
import com.hl.card.modular.packBack.service.SuperPartnerActiveService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 发布会报名人员 前端控制器
 * </p>
 *
 * @author jason
 * @since 2018-05-31
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/enrolment")
public class EnrolmentController {
    @Autowired
    SuperPartnerActiveService superPartnerActiveService;
    @Autowired
    private IEnrolmentService enrolmentService;

    @PostMapping
    public ResponseObject save(Enrolment enrolment){
        ResponseObject res=new ResponseObject();
        try{
            Boolean b=enrolmentService.insert(enrolment);
            if(b){
                res.setErroCode(0);
                res.setData("添加成功");
            }else {
                res.setErroCode(1);
                res.setData("添加失败");
            }
        }catch (Exception e){
            res.setErroCode(1);
            res.setData("添加失败");
        }
        return res;
    }

    @GetMapping("isExist/{phone}")
    public ResponseObject isExist( @PathVariable("phone") String phone){
        ResponseObject res=null;
        Map<String,Object> map=new HashMap<>();
        map.put("phone",phone);
        try {
            List<Enrolment> re = enrolmentService.selectByMap(map);
            if(re.isEmpty()){
                res= ResponseUtil.ERROR("手机号未注册");
            }else{
                res=ResponseUtil.SUCCESS("手机号已注册");
            }
        }catch (Exception e){
            e.printStackTrace();
            res=ResponseUtil.ERROR("系统错误");
        }
        return res;
    }

    @PutMapping
    public ResponseObject update( Enrolment enrolment){
        ResponseObject res=null;
        enrolment.setStatus(1);
        Map<String,Object> map=new HashMap<>();
        try {
            if(StringUtils.isBlank(enrolment.getPhone())){
                res=ResponseUtil.SUCCESS("请输入手机号");
                return res;
            }
            map.put("phone",enrolment.getPhone());
            List<Enrolment> re = enrolmentService.selectByMap(map);
            if(re.isEmpty()){
                res=ResponseUtil.ERROR("手机号未注册");
            }else{
                Wrapper<Enrolment> enrolmentWrapper=new EntityWrapper();
                enrolmentWrapper.eq("phone",enrolment.getPhone());
                Boolean b = enrolmentService.update(enrolment,enrolmentWrapper);
                res=ResponseUtil.SUCCESS("签到成功");
            }
        }catch (Exception e){
            res=ResponseUtil.ERROR("系统错误");
        }
        return res;
    }
    @RequestMapping("/queryActiveCode")
    @ResponseBody
    public Object queryActiveCode(String phone){
        String activeCode = superPartnerActiveService.queryActiveCode(phone);
        Map<String ,Object> map = new HashMap<>();
        if(activeCode == null){
            map.put("code",201);
            map.put("errmsg","未找到该手机下的验证码");
        }else{
            map.put("code",200);
            map.put("activecode",activeCode);
        }
        return map;
    }

}

